package com.hai.util;

import java.net.Socket;
import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

public class HttpUtil {
	// 创建HttpClient对象
	public static HttpClient httpClient = new DefaultHttpClient();
	public static int UID;//通信常用到当前用户ID
	public static String UNAME;
	public static final String SERVERIP ="192.168.191.1";
	public static final int SERVERPORT =30000;
	public static final String BASE_URL = "http://"+SERVERIP+":8080/QTalkService/visit";
	public static Socket socket=null;

	// "http://www.qtalkservice.sinaapp.com/";
	/**
	 *
	 * @param url
	 *            发送请求的URL
	 * @return 服务器响应字符串
	 * @throws Exception
	 */
	public static String getRequest(final String url) throws Exception {
		FutureTask<String> task = new FutureTask<String>(
				new Callable<String>() {
					@Override
					public String call() throws Exception {
						// 创建HttpGet对象。
						HttpGet get = new HttpGet(url);
						// 发送GET请求
						HttpResponse httpResponse = httpClient.execute(get);
						// 如果服务器成功地返回响应
						if (httpResponse.getStatusLine().getStatusCode() == 200) {
							// 获取服务器响应字符串
							String result = EntityUtils.toString(httpResponse
									.getEntity());
							return result;
						}
						return null;
					}
				});
		new Thread(task).start();
		return task.get();
	}

	/**
	 * @param url
	 *            发送请求的URL
	 * @param params
	 *            请求参数
	 * @return 服务器响应字符串
	 * @throws Exception
	 */
	public static String postRequest(final String url, final String jsonString)
			throws Exception {
		FutureTask<String> task = new FutureTask<String>(
				new Callable<String>() {
					@Override
					public String call() throws Exception {
						// 创建HttpPost对象。
						HttpPost post = new HttpPost(url);
						StringEntity s = new StringEntity(jsonString);
						s.setContentEncoding("UTF-8");
						s.setContentType("application/json");
						post.setEntity(s);
						// 发送POST请求
						HttpResponse httpResponse = httpClient.execute(post);
						// 如果服务器成功地返回响应
						if (httpResponse.getStatusLine().getStatusCode() == 200) {
							// 获取服务器响应字符串
							String entity = EntityUtils.toString(httpResponse
									.getEntity());

//							Map map=new HashMap();
//							//map.put("json", entity);
//							Gson g = new Gson();  //上面获取到的entity已是json格式，服务器端已封装好的，这里再封一次就不是json格式了
//							Map resJsonString = g.fromJson(entity, Map.class);
//							System.out.println("---为"+map.get("friends"));
							//response = JSONObject.fromObject(entity);
							return entity;
						}
						return null;
					}
				});
		new Thread(task).start();
		return task.get();
	}

}